<template>
  <view class="goods-detail">
    <!-- 1 轮播图 -->
    <u-swiper
      @click="handlePreviewImage"
      bg-color="#fff"
      :list="goods.pics"
      name="pics_big"
      height="497"
      img-mode="aspectFit"
    ></u-swiper>
    <!-- 2 价格 -->
    <view class="goods-price">￥{{ goods.goods_price }}</view>
    <!-- 3 名称 -->
    <view class="u-p-15">
      <view class="goods-name u-line-2">{{ goods.goods_name }}</view>
    </view>
    <u-line color="#666"></u-line>
    <!-- 4 图文详情 -->
    <view class="introduce">
      <view class="introduce-title u-p-15 u-text-center">图文详情 </view>
      <view class="introduce-content">
        <!-- 方式一 -->
        <!-- <view v-html="goods.goods_introduce"></view> -->
        <!-- 方式二 -->
        <!-- <rich-text :nodes="goods.goods_introduce"></rich-text> -->
        <!-- 方式三 -->
        <u-parse :html="goods.goods_introduce"></u-parse>
      </view>
    </view>

    <!-- 5 提交订单栏 -->

    <view class="navigation">
      <view class="left">
        <view class="item">
          <u-icon
            name="server-fill"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">客服</view>
        </view>
        <view class="item">
          <u-icon
            name="home"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">店铺</view>
        </view>
        <navigator
          open-type="switchTab"
          url="/pages/cart/cart"
          class="item car"
        >
          <u-badge
            class="car-num"
            :count="count"
            type="error"
            :offset="[-3, -6]"
          ></u-badge>
          <u-icon
            name="shopping-cart"
            :size="40"
            :color="$u.color['contentColor']"
          ></u-icon>
          <view class="text u-line-1">购物车</view>
        </navigator>
      </view>
      <view class="right">
        <view class="cart btn u-line-1" @click="handleCartAdd">加入购物车</view>
        <view class="buy btn u-line-1">立即购买</view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      goods: null,
    };
  },
  onLoad({ goods_id }) {
    goods_id = goods_id || 47869;
    this.getGoodsDetails(goods_id);
  },
  computed: {
    ...mapState({
      // 总的数量
      count: (state) =>
        state.cart.cartList.reduce((count, item) => (count += item.nums), 0),
    }),
  },
  methods: {
    // 获取vuex mutatations
    ...mapMutations(["cart/cartAdd"]),
    async getGoodsDetails(goods_id) {
      this.goods = await this.$u.get("/goods/detail", { goods_id });
    },
    // 轮播图点击事件
    handlePreviewImage(index) {
      const urls = this.goods.pics.map((pic) => pic.pics_big);
      // 当前点击的图片索引
      const current = urls[index];
      // 调用api 放大预览
      uni.previewImage({ urls, current });
    },
    // 点击加入购物车
    handleCartAdd() {
      this["cart/cartAdd"]({ value: this.goods });
    },
  },
};
</script>

<style lang="scss" scoped>
.goods-detail {
  padding-bottom: 102rpx;
}
.goods-price {
  padding: 10rpx;
  color: $themeColor;
}
.goods-name {
  // padding: 10rpx;
  color: #666;
}
.navigation {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  display: flex;
  margin-top: 100rpx;
  border: solid 2rpx #f2f2f2;
  background-color: #ffffff;
  padding: 16rpx 0;
  .left {
    display: flex;
    font-size: 20rpx;
    .item {
      margin: 0 30rpx;
      &.car {
        text-align: center;
        position: relative;
        .car-num {
          position: absolute;
          top: -10rpx;
          right: -10rpx;
        }
      }
    }
  }
  .right {
    display: flex;
    font-size: 28rpx;
    align-items: center;
    .btn {
      line-height: 66rpx;
      padding: 0 30rpx;
      border-radius: 36rpx;
      color: #ffffff;
    }
    .cart {
      background-color: #ed3f14;
      margin-right: 30rpx;
    }
    .buy {
      background-color: #ff7900;
    }
  }
}
</style>